#include "d3d9/d3d9display.h"

using namespace Kraw;

void Display::SetTransform( Display::TransformType type, const matrix44& mat )
{
	D3D9Display* self = (D3D9Display*) this;

	ASSERT(type<Display::Max_TransformTypes);
	self->matTransform[type] = mat;
	//TODO- mark dependent transforms as dirty
}

const matrix44& Display::GetTransform( TransformType type )
{
	D3D9Display* self = (D3D9Display*) this;

	ASSERT(type<Max_TransformTypes);
    switch (type)
    {
    case WorldView:
        self->matTransform[WorldView] = self->matTransform[World] * self->matTransform[View];
        break;
    case WorldViewProjection:
        self->matTransform[WorldViewProjection] = self->GetTransform(WorldView) * self->matTransform[Projection];
        break;
    default:
        Log::Write("Display::GetTransform() : unsupported transform!");
    }

    return self->matTransform[type];
}

/// initialize transforms to meaningful default values
void D3D9Display::InitTransforms()
{
	matTransform[Display::World] = identity4();
	matTransform[Display::View] = identity4();
	matTransform[Display::Projection] = perspectiveFovLh(PI/4, 4.f/3.f, 1.f, 1000.f);
}

/// TODO- update all transforms that changed
/// if not using the fixed function, update shader constants
void D3D9Display::SetTransforms()
{
    D3D9Display* self = (D3D9Display*) this;

	if (self->pCurShader)
	{
		//for (all types defined for the current shader)
		//StartRegister= initial register of this type parameter in the current shader
		//self->pD3D9Device->SetVertexShaderConstantF( StartRegister, (float*) &self->transforms[type], 4 );
	}
	else
	{
		self->pD3D9Device->SetTransform( D3DTS_WORLD, (D3DMATRIX*) &self->matTransform[Display::World] );
		self->pD3D9Device->SetTransform( D3DTS_VIEW, (D3DMATRIX*) &self->matTransform[Display::View] );
		self->pD3D9Device->SetTransform( D3DTS_PROJECTION, (D3DMATRIX*) &self->matTransform[Display::Projection] );
	}
}
